<?php
header('Content-type:text/html;charset=utf8');
/**
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-3-1
*/
/**
 * 
 * 品牌控制器
 * @author xjh
 *
 */
class BrandController extends Controller{
	/**
	 * 访问控制过滤
	 * @author xjh
	 */
	function filters(){
		return array(
			'accessControl'
		);
	}
	/**
	 * 具体方法访问具体条件
	 */
	
	function accessRules(){
		return array(
			array(
				'allow',
				'actions'=>array('brand','show','update','del','add','addbrand','sort','brandupd'),
				'users'=>array('@'),//登陆系统的用户
			),
//			array(
//				'deny',
//				'users'=>array('*'),//所有用户
//			),
		);
	}
	/**
	 * 顶部信息
	 * 
	 */
	function actionBrandTop(){
		$this->renderPartial('brandTop');
	}
	/**
	 * 品牌删除
	 */
	function actionBrandDel($brand_id){
		$brand_model = Brand::model();
		$brand_model->BrandDel($brand_id);
		$this->redirect('./index.php?r=backweb/brand/brand');
	}
	/**
	 * 品牌修改
	 * @param $brand_id
	 */
	function actionBrandUpd($brand_id){
		$recPost = $_POST;
		$category_model = new Category();
		$brand_model =  Brand::model();
		$infos = $category_model->all_category();
		$info = $brand_model->getOneBrand($brand_id);
		$info = $info[0];
		if(isset($recPost) && @$recPost['send'] == "修改"){
			$row = $brand_model->BrandUpdate($recPost,$brand_id);
			if($row){
				$this->redirect("./index.php?r=backweb/brand/brand");
			}else{
				echo "<script>alert('修改失败');</script>";
			}
		}
		$this->renderPartial('updatebrand',array('info'=>$info,'infos'=>$infos));
	}
	
	/**
	 * 搜索模块
	 */
	
	function actionSearch(){
		$brand_model = Brand::model();
		$result = $brand_model->Search();
		print_r($result);
	}
	
	/**
	 * 是否有效的判断
	 */
	function actionActive($brand_id,$active_id){
		$active = new active();
		$active -> setActive($brand_id,$active_id,"brand","brand_id");
		$this->redirect("./index.php?r=backweb/brand/brand");
	}
	
	
	
	/**
	 * 商品品牌控制器
	 * @author xjh
	 */
	function actionBrand($category_id = 0){
		$brand_model = Brand::model();
		$infos = $brand_model->pageSearch($category_id);
		$page_list = $brand_model->page_list;
		$infos_count = $brand_model->infos_count;
		$a = $brand_model->categoryAll();
		$active = array(
			0=>'否',
			1=>'是',
		);
		$obj = new CDbConnection();
		 
		$topInfo = $obj -> getStats();
		$number = $topInfo[0];
		$times = $topInfo[1];
		$this->renderPartial('brand',array('infos'=>$infos,'info'=>$a,'active'=>$active,'info_count'=>$infos_count[0]['count(*)'],'page_list'=>$page_list,'number'=>$number,'times'=>$times));
	}
	/**
	 * 品牌排序设定
	 */
	function actionSort(){
		if(@isset($_POST) && $_POST['send'] == "排序"){
			$brand_model = new Brand();
			$dataAll = array_combine($_POST['brand_id'],$_POST['sort']);
			$brand_model->sort($dataAll);
			$this->redirect("./index.php?r=backweb/brand/brand");
		}
	}
	function actiontoAddBrand(){
		
		$category_model = Category::model();
		$info = $category_model->all_category();
		
		$this->renderPartial('addbrand',array('category'=>$info));
	}
	/*
	 * 品牌添加控制器
	 * @author xjh
	 */
	function actionAddBrand(){
		$addInfo  = $_POST;
		if(!empty($addInfo['brand_thumbnail_url'])){
			$image = new Image($addInfo['brand_thumbnail_url']);
			if($addInfo['brand_cover'] == 1){
				$image->fixPro(300,300);
			}else{
				$image->fixPro(140,80);
			}
			$image->out();
		}
		
		$category_model = Category::model();
		$brand_model = new Brand();
		$infos = $brand_model->checkExist($addInfo);
		$info = $category_model->all_category();
		if(isset($addInfo)){
			if($_POST['send'] == '添加'){
				if(!empty($infos)){
					echo "<script>alert('商品已存在');</script>";
					echo "操作错误，请返回";
					exit;
				}
				if($brand_model->addBrand($addInfo)){
					echo "<script>alert('品牌添加成功');</script>";
					 $this->renderPartial('brandtop');
				}else{
					echo "<script>alert('添加失败');</script>";
				}
			}
		}
		
	}
	
	
	
	
	//单文件上传
	function actionthumbnail(){
		$this->renderPartial('thumbnail');
	}
	
	//单文件上传处理
	function actionuploadfile(){ 
		if(isset($_POST['send'])){
			$_fileupload = new upload('pic',$_POST['MAX_FILE_SIZE']);
			$this->alertOpenerClose("缩略图上传成功",$_fileupload->getPath());
		}else{
			echo "<script>alert('文件过大,请重新上传')</script>";
			exit;
		}
	}
	
	//上传成功返回填写地址
	function alertOpenerClose($_info,$_path){
		echo "<script type='text/javascript'>alert('$_info');</script>";
		echo "<script type='text/javascript'>opener.document.fm.brand_thumbnail_url.value='$_path';</script>";
		echo "<script type='text/javascript'>opener.document.fm.pic.style.display='block';</script>";
		echo "<script type='text/javascript'>opener.document.fm.pic.src='$_path';</script>";
		echo "<script type='text/javascript'>window.close()</script>";
		exit();
	}
}
?>